﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            Func10();


            Console.ReadKey();
        }

        /// <summary>
        /// 找出数组中的所有偶数
        /// </summary>
        static void Func1()
        {
            int[] numbers = { 1, 3, 4, 6, 7, 8, 9, 2 };
            List<int> evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
            foreach (int number in evenNumbers)
            {
                Console.WriteLine(number);
            }
        }


        /// <summary>
        /// 合并两个数组中的偶数
        /// </summary>
        static void Func2()
        {
            int[] array1 = { 1, 3, 4, 6, 7, 8, 9, 2 };
            int[] array2 = { 12,14,16,13,15,17,19,20 };
            var evenNumbers1 = array1.Where(n => n % 2 == 0).ToList();
            var evenNumbers2 = array2.Where(n => n % 2 == 0).ToList();
            var mergedEvenNumbers = evenNumbers1.Union(evenNumbers2).ToList();
            foreach (int number in mergedEvenNumbers)
            {
                Console.WriteLine(number);
            }
        }

        static void Func3()
        {
            int[] array1 = { 1, 3, 4, 6, 7, 8, 9, 2 };
            int[] array2 = { 12, 14, 16, 13, 15, 17, 19, 20 };
            var sums = array1.Zip(array2, (a, b) => a + b).ToList();
            foreach (var sum in sums)
            {
                Console.WriteLine(sum);
            }
        }

        /// <summary>
        /// 对数组中的元素进行判断，所有元素大于5
        /// </summary>
        static void Func4()
        {
            int[] numbers = { 1, 3, 4, 6, 7, 8, 9, 2 };
            bool b = numbers.All(n => n > 5);
            if (b)
            {
                Console.WriteLine("所有元素大于5");
            }
            else
            {
                Console.WriteLine("不是所有元素大于5");
            }
        }

       /// <summary>
       /// 
       /// </summary>
        static void Func5()
        {
            int[] numbers = { 1, 3, 4, 6, 7, 8, 9, 2 };
            bool b = numbers.Any(n => n > 5);
            if (b)
            {
                Console.WriteLine("存在元素大于5");
            }
            else
            {
                Console.WriteLine("不存在元素大于5");
            }
        }

        /// <summary>
        /// 给数组添加元素，生成新的数组，原数组没有改变
        /// </summary>
        static void Func6()
        {
            int[] numbers = { 1,2,3,4 };

            var numbers2 = numbers.Append(5);
            foreach (var sum in numbers)
            {
                Console.WriteLine(sum);
            }

            Console.WriteLine("======================");

            foreach (var sum in numbers2)
            {
                Console.WriteLine(sum);
            }

        }

        /// <summary>
        /// 求平均值
        /// </summary>
        static void Func7()
        {
            int[] numbers = { 1, 2, 3, 4, 8, 10};
            var a = numbers.Where(n => n % 2 == 0).Average();
            Console.WriteLine(a);
        }


        static void Func8()
        {
            int[] numbers = { 1, 2, 3, 4, 8, 10 };
            var a = numbers.Reverse().FirstOrDefault();
            Console.WriteLine(a);
        }

       

        static void Func9()
        {
            List<Person> people = GetPersons();
            IEnumerable<Pair> o = people.GroupBy(p => p.Addr)
                   .Select(g => new Pair(){
                       Addr = g.Key,
                       Count = g.Count()
                   });
            foreach(Pair a in o)
            {
                Console.WriteLine(a);
            }
        }

        static void Func10()
        {
            List<Person> people = GetPersons();
            var maxAgePerson = people.OrderByDescending(p => p.Age).FirstOrDefault();
            Console.WriteLine(maxAgePerson);


            var maxAgePersonGroup = people.GroupBy(p => p.Age).OrderByDescending(g => g.Key).First();
            var maxAgePerson2 = maxAgePersonGroup.Key; // 这是年龄  
            Console.WriteLine(maxAgePerson2);
            var personWithMaxAge = maxAgePersonGroup.FirstOrDefault(); // 这是年龄最大的人
            Console.WriteLine(personWithMaxAge);
        }



        public static List<Person> GetPersons()
        {
            List<Person> list = new List<Person>();

            list.Add(new Person() { Id = 1,Name = "刘德华",Age = 55,Sex = "男",Addr = "香港"});
            list.Add(new Person() { Id = 2,Name = "张学友",Age = 60,Sex = "男",Addr = "香港"});
            list.Add(new Person() { Id = 3,Name = "黎明",Age = 60,Sex = "男",Addr = "香港"});
            list.Add(new Person() { Id = 4,Name = "郭富城",Age = 35,Sex = "男",Addr = "香港"});
            list.Add(new Person() { Id = 5,Name = "陈道明",Age = 65,Sex = "男",Addr = "北京"});
            list.Add(new Person() { Id = 6,Name = "张毅",Age = 45,Sex = "男",Addr = "河北"});
            list.Add(new Person() { Id = 7,Name = "高启强",Age = 45,Sex = "男",Addr = "广东"});
            list.Add(new Person() { Id = 8,Name = "高启胜",Age = 35,Sex = "男",Addr = "广东"});

            return list;
        }

    }

    internal class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public string Addr { get; set; }
        public int Age { get; set; }

        public override string ToString()
        {
            return $"Id:{Id},Name:{Name},Sex:{Sex},Addr:{Addr},Age:{Age}";
        }
    }

    internal class Pair
    {
        public string Addr { get; set; }

        public int Count { get; set; }

        public override string ToString()
        {
            return $"Addr:{Addr},Count:{Count}";
        }
    }
    
}
